--package.path = package.path..";?.lua"
package.path = "./plugin/GPE48300C/utils.lua"..";..\\?.lua"
package.path = "./plugin/GPE48300C/json.lua"..";..\\?.lua"
package.path = "./plugin/GPE48300C/ydt1363.lua"..";..\\?.lua"

require ("utils")
require ("json")
require ("ydt1363")

-- rxBuf= {
--     0x7E,0x30,0x31,0x34,0x30,0x30,0x30,0x31,0x30,0x30,
--     0x39,0x32,0x34,0x30,0x30,0x30,0x30,0x30,0x30,0x30,
--     0x39,0x32,0x39,0x30,0x30,0x30,0x30,0x30,0x30,0x30,
--     0x39,0x31,0x44,0x30,0x30,0x30,0x30,0x30,0x30,0x33,
--     0x31,0x43,0x42,0x0D
-- }
rxBuf = {}
regAddr = 0x000000

function NewVariables()
    -- body
    local VariableMap = {
        Variable = {
        },
    }

    return VariableMap
end

function GenerateGetRealVariables(sAddr,step)

    -- body
    local requestADU = {}
    local requestADULen = 0
    local requestPDU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(step == 0)
    then
        ----------交流模块获取模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x40
        requestPDU[3] = 0x01
        requestPDU[4] = 0x00

        --获取校验和码
        local requestPDULen = 4
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x4001
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }
    elseif(step == 1)
    then
        ----------整流模块获取设备1模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x01

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410101
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }      
    elseif(step == 2)
    then
        ----------整流模块获取设备2模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x02

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410102
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }    
    elseif(step == 3)
    then
        ----------整流模块获取设备2模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x03

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410103
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }  
    elseif(step == 4)
    then
        ----------整流模块获取设备2模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x04

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410104
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }   
    elseif(step == 5)
    then
        ----------整流模块获取设备2模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x05

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410105
        VariableMap = {
            Status = "1",
            Variable = requestADU
        } 
    elseif(step == 6)
    then
        ----------整流模块获取设备2模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x06

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410106
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }
    elseif(step == 7)
    then
        ----------直流模块获取模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x42
        requestPDU[3] = 0x01
        requestPDU[4] = 0x00

        --获取校验和码
        local requestPDULen = 4
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x4201
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    end

    -- local tmp = ""
    -- for i=1,requestADULen,1 do
    --     tmp = tmp .. " " .. string.format("%02X",requestADU[i])
    -- end
    -- print(tmp)

    return VariableMap
end

function DeviceCustomCmd(sAddr,cmdName,cmdParam,step)

    -- body
    local requestADU = {}
    local requestADULen = 0
    local requestPDU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(cmdName == "GetRealVariables")
    then
        if(step == 0)
    then
        ----------交流模块获取模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x40
        requestPDU[3] = 0x01
        requestPDU[4] = 0x00

        --获取校验和码
        local requestPDULen = 4
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x4001
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }
    elseif(step == 1)
    then
        ----------整流模块获取设备1模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x01

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410101
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }      
    elseif(step == 2)
    then
        ----------整流模块获取设备2模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x02

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410102
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }    
    elseif(step == 3)
    then
        ----------整流模块获取设备2模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x03

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410103
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }  
    elseif(step == 4)
    then
        ----------整流模块获取设备2模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x04

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410104
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }   
    elseif(step == 5)
    then
        ----------整流模块获取设备2模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x05

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410105
        VariableMap = {
            Status = "1",
            Variable = requestADU
        } 
    elseif(step == 6)
    then
        ----------整流模块获取设备2模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x41
        requestPDU[3] = 0x01
        requestPDU[4] = 0x01
        requestPDU[5] = 0x06

        --获取校验和码
        local requestPDULen = 5
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x410106
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }     
    elseif(step == 7)
    then
        ----------直流模块获取模拟量数据-----------------
        requestPDU[1] = sAddr
        requestPDU[2] = 0x42
        requestPDU[3] = 0x01
        requestPDU[4] = 0x00

        --获取校验和码
        local requestPDULen = 4
        local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

        --往缓存中加入SOI
        table.insert(requestADU,0x7E)
        requestADULen = requestADULen + 1

        --往缓存中加入VER~INFO数据
        local requestPDUStr = {}
        requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
        for index=1,2*requestPDULen,1 do
            table.insert(requestADU,requestPDUStr[index])
        end
        requestADULen = requestADULen + 2*requestPDULen

        --往缓存中加入CHKSUM
        for index=1,2,1 do
            table.insert(requestADU,chksum[index])
        end
        requestADULen = requestADULen + 2

        --往缓存中加入EOI
        table.insert(requestADU,0x0D)
        requestADULen = requestADULen + 1

        regAddr = 0x4201
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    end
    elseif(cmdName == "SetOpen")
    then
    elseif(cmdName == "SetClose")
    then           
    end

    return VariableMap
end

function AnalysisRx(sAddr,rxBufCnt)

    local SOI,VER,ADR,CID1,CID2,LENGTH,LENID,INFO,DATA,CHKSUM,EOI

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    --SOI[1]+ADR[2]+CID1[2]+CID2[2]+LENGTH[2]+CHKSUM[2]+EOI[1]
    if(rxBufCnt < 12)
    then
        rxBuf = {}
        return VariableMap
    end

    for index=1,rxBufCnt,1 do
        if(index <= rxBufCnt)
        then
            SOI  = rxBuf[index]
            if(SOI==0x7E and rxBufCnt>=12) then
                ADR  = utils.Str2Hex(utils.Copy(rxBuf,index+1,2),2)
                CID1 = utils.Str2Hex(utils.Copy(rxBuf,index+3,2),2)
                CID2 = utils.Str2Hex(utils.Copy(rxBuf,index+5,2),2)
                LENGTH = utils.Str2Hex(utils.Copy(rxBuf,index+7,2),2)
                print("ADR "..string.format("%02X",ADR[1]))
                print("CID1 "..string.format("%02X",CID1[1]))
                print("CID2 "..string.format("%02X",CID2[1]))
                print("LENGTH "..string.format("%02X",LENGTH[1]))

                --CHKSUM校验
                if(ydt1363.CheckCHKSUM(rxBuf,index+LENGTH[1]*2+8) == 1) then
                    rxBuf = {}
                    VariableMap = {
                        Status = "1",
                        Variable = {}
                    }
                    return VariableMap  
                end

                --INFO字段偏移量
                INFO = utils.Str2Hex(utils.Copy(rxBuf,index+9,LENGTH[1]*2),LENGTH[1]*2)
                --判断设备标识码和控制标识码
                if(CID1[1]==0x40 and CID2[1]==0x00) then
                    if(regAddr == 0x4001) then
                        local Variable = {}
                        local VariableIndex = 1
                        local DATAIndex = 1
                        --获取模拟量数据
                        --------------------------A相电压------------------------------------------------
                        local VoltageA = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"VoltageA",VoltageA,VoltageA)
                        VariableIndex = VariableIndex + 1
                        ------------------------A相电流------------------------------------------------
                        local CurrentA = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CurrentA",CurrentA,CurrentA)
                        VariableIndex = VariableIndex + 1
                        --------------------------A相告警------------------------------------------------
                        local AlarmA = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        --------------------------A相频率低----------------
                        local LowFreqAlarmA = utils.And(AlarmA,0x80)
                        if(LowFreqAlarmA == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"LowFreqAlarmA",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"LowFreqAlarmA",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------A相频率高-----------------
                        local HighFreqAlarmA = utils.And(AlarmA,0x40)
                        if(HighFreqAlarmA == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighFreqAlarmA",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighFreqAlarmA",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------A相交流空开----------------
                        local ACOpenAlarmA = utils.And(AlarmA,0x20)
                        if(ACOpenAlarmA == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"ACOpenAlarmA",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"ACOpenAlarmA",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------A相频电流高---------------
                        local HighCurrentAlarmA = utils.And(AlarmA,0x10)
                        if(HighCurrentAlarmA == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighCurrentAlarmA",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighCurrentAlarmA",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------A相交流停电---------------
                        local ACFailAlarmA = utils.And(AlarmA,0x08)
                        if(ACFailAlarmA == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"ACFailAlarmA",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"ACFailAlarmA",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------A相电压高----------------
                        local HighVoltageAlarmA = utils.And(AlarmA,0x04)
                        if(HighVoltageAlarmA == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighVoltageAlarmA",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighVoltageAlarmA",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------A相电压低----------------
                        local LowVoltageAlarmA = utils.And(AlarmA,0x02)
                        if(LowVoltageAlarmA == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"LowVoltageAlarmA",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"LowVoltageAlarmA",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------A相缺相------------------
                        local OpenPhaseAlarmA = utils.And(AlarmA,0x01)
                        if(OpenPhaseAlarmA == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OpenPhaseAlarmA",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OpenPhaseAlarmA",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------B相电压------------------------------------------------
                        local VoltageB = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"VoltageB",VoltageB,VoltageB)
                        VariableIndex = VariableIndex + 1
                        --------------------------B相电流------------------------------------------------
                        local CurrentB = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CurrentB",CurrentB,CurrentB)
                        VariableIndex = VariableIndex + 1
                        --------------------------B相告警------------------------------------------------
                        local AlarmB = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        --------------------------B相电流高---------------
                        local HighCurrentAlarmB = utils.And(AlarmB,0x10)
                        if(HighCurrentAlarmB == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighCurrentAlarmB",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighCurrentAlarmB",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------B相电压高----------------
                        local HighVoltageAlarmB = utils.And(AlarmB,0x04)
                        if(HighVoltageAlarmB == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighVoltageAlarmB",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighVoltageAlarmB",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------B相电压低----------------
                        local LowVoltageAlarmB = utils.And(AlarmB,0x02)
                        if(LowVoltageAlarmB == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"LowVoltageAlarmB",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"LowVoltageAlarmB",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------B相缺相------------------
                        local OpenPhaseAlarmB = utils.And(AlarmB,0x01)
                        if(OpenPhaseAlarmB == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OpenPhaseAlarmB",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OpenPhaseAlarmB",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------C相电压------------------------------------------------
                        local VoltageC = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"VoltageC",VoltageC,VoltageC)
                        VariableIndex = VariableIndex + 1
                        --------------------------C相电流------------------------------------------------
                        local CurrentC = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"CurrentC",CurrentC,CurrentC)
                        VariableIndex = VariableIndex + 1
                        --------------------------C相告警------------------------------------------------
                        local AlarmC = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        --------------------------C相电流高---------------
                        local HighCurrentAlarmC = utils.And(AlarmC,0x10)
                        if(HighCurrentAlarmC == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighCurrentAlarmC",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighCurrentAlarmC",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------C相电压高----------------
                        local HighVoltageAlarmC = utils.And(AlarmC,0x04)
                        if(HighVoltageAlarmC == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighVoltageAlarmC",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"HighVoltageAlarmC",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------C相电压低----------------
                        local LowVoltageAlarmC = utils.And(AlarmC,0x02)
                        if(LowVoltageAlarmC == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"LowVoltageAlarmC",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"LowVoltageAlarmC",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------C相缺相------------------
                        local OpenPhaseAlarmC = utils.And(AlarmC,0x01)
                        if(OpenPhaseAlarmC == 0) then
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OpenPhaseAlarmC",0,"0")
                        else 
                            Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OpenPhaseAlarmC",1,"1")
                        end 
                        VariableIndex = VariableIndex + 1
                        --------------------------频率-----------------------------------------------
                        local Freq = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"Freq",Freq,Freq)
                        VariableIndex = VariableIndex + 1
                        
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap 
                    end
                elseif(CID1[1]==0x41 and CID2[1]==0x00) then
                    print("regAddr "..string.format("%03X",regAddr))
                    if(regAddr == 0x410101) then
                        local Variable = {}
                        local VariableIndex = 1
                        local DATAIndex = 1
                        --整流模块获取设备1模拟量数据
                        --------------------------模块地址------------------------------------------------
                        local Addr = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        --------------------------输出电压------------------------------------------------
                        local OutVoltage1 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutVoltage1",OutVoltage1,OutVoltage1)
                        VariableIndex = VariableIndex + 1
                        ------------------------输出电流------------------------------------------------
                        local OutCurrent1 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"OutCurrent1",OutCurrent1,OutCurrent1)
                        VariableIndex = VariableIndex + 1
                        
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap        
                    elseif(regAddr == 0x410102) then
                        local Variable = {}
                        local VariableIndex = 1
                        local DATAIndex = 1
                        --整流模块获取设备2模拟量数据
                        --------------------------模块地址------------------------------------------------
                        local Addr = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        --------------------------输出电压------------------------------------------------
                        local OutVoltage2 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutVoltage2",OutVoltage2,OutVoltage2)
                        VariableIndex = VariableIndex + 1
                        ------------------------输出电流------------------------------------------------
                        local OutCurrent2 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"OutCurrent2",OutCurrent2,OutCurrent2)
                        VariableIndex = VariableIndex + 1
                        
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap                         
                    elseif(regAddr == 0x410103) then
                        local Variable = {}
                        local VariableIndex = 1
                        local DATAIndex = 1
                        --整流模块获取设备3模拟量数据
                        --------------------------模块地址------------------------------------------------
                        local Addr = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        --------------------------输出电压------------------------------------------------
                        local OutVoltage3 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutVoltage3",OutVoltage3,OutVoltage3)
                        VariableIndex = VariableIndex + 1
                        ------------------------输出电流------------------------------------------------
                        local OutCurrent3 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"OutCurrent3",OutCurrent3,OutCurrent3)
                        VariableIndex = VariableIndex + 1
                        
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap                         
                    elseif(regAddr == 0x410104) then
                        local Variable = {}
                        local VariableIndex = 1
                        local DATAIndex = 1
                        --整流模块获取设备4模拟量数据
                        --------------------------模块地址------------------------------------------------
                        local Addr = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        --------------------------输出电压------------------------------------------------
                        local OutVoltage4 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutVoltage4",OutVoltage4,OutVoltage4)
                        VariableIndex = VariableIndex + 1
                        ------------------------输出电流------------------------------------------------
                        local OutCurrent4 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"OutCurrent4",OutCurrent4,OutCurrent4)
                        VariableIndex = VariableIndex + 1
                        
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap                         
                    elseif(regAddr == 0x410105) then
                        local Variable = {}
                        local VariableIndex = 1
                        local DATAIndex = 1
                        --整流模块获取设备5模拟量数据
                        --------------------------模块地址------------------------------------------------
                        local Addr = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        --------------------------输出电压------------------------------------------------
                        local OutVoltage5 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutVoltage5",OutVoltage5,OutVoltage5)
                        VariableIndex = VariableIndex + 1
                        ------------------------输出电流------------------------------------------------
                        local OutCurrent5 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"OutCurrent5",OutCurrent5,OutCurrent5)
                        VariableIndex = VariableIndex + 1
                        
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap                         
                    elseif(regAddr == 0x410106) then
                        local Variable = {}
                        local VariableIndex = 1
                        local DATAIndex = 1
                        --整流模块获取设备6模拟量数据
                        --------------------------模块地址------------------------------------------------
                        local Addr = INFO[DATAIndex]
                        DATAIndex = DATAIndex + 1
                        --------------------------输出电压------------------------------------------------
                        local OutVoltage6 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutVoltage6",OutVoltage6,OutVoltage6)
                        VariableIndex = VariableIndex + 1
                        ------------------------输出电流------------------------------------------------
                        local OutCurrent6 = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"OutCurrent6",OutCurrent6,OutCurrent6)
                        VariableIndex = VariableIndex + 1
                        
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap                         
                    end  
                elseif(CID1[1]==0x42 and CID2[1]==0x00) then
                    if(regAddr == 0x4201) then
                        local Variable = {}
                        local VariableIndex = 1
                        local DATAIndex = 1
                        --获取模拟量数据
                        --------------------------母排电压------------------------------------------------
                        local BusVoltage = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"BusVoltage",BusVoltage,BusVoltage)
                        VariableIndex = VariableIndex + 1
                        ------------------------电池电流------------------------------------------------
                        local BatteryCurrent = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"BatteryCurrent",BatteryCurrent,BatteryCurrent)
                        VariableIndex = VariableIndex + 1
                        ------------------------负载总电流----------------------------------------------
                        local LoadCurrent = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"LoadCurrent",LoadCurrent,LoadCurrent)
                        VariableIndex = VariableIndex + 1
                        ------------------------负载1电流------------------------------------------------
                        DATAIndex = DATAIndex + 2
                        ------------------------负载2电流------------------------------------------------
                        DATAIndex = DATAIndex + 2
                        ------------------------负载3电流------------------------------------------------
                        DATAIndex = DATAIndex + 2
                        ------------------------负载4电流------------------------------------------------
                        DATAIndex = DATAIndex + 2
                        ------------------------电池1电流------------------------------------------------
                        DATAIndex = DATAIndex + 2
                        ------------------------电池2电流------------------------------------------------
                        DATAIndex = DATAIndex + 2
                        ------------------------环境温度------------------------------------------------
                        local Temperature = INFO[DATAIndex]*256+INFO[DATAIndex+1]
                        DATAIndex = DATAIndex + 2
                        Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"Temperature",Temperature,Temperature)
                        VariableIndex = VariableIndex + 1
                        ------------------------环境湿度------------------------------------------------
                        ------------------------整流模块数量------------------------------------------------
                        
                        
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap 
                    end    
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end

--GenerateGetRealVariables(1,1)
--AnalysisRx(1,34)